package util;

import java.util.HashMap;
import java.util.Set;

public class savedResultWrapper {

	public int id;
	public int targetId;
	public String name;
	HashMap<String, Double> valueMap = new HashMap<String, Double>();

	public void populateMap(String values) {
		String[] keyValuePairs = values.split(";;");
		for(String keyValue: keyValuePairs){
			String[] splitKeyValue = keyValue.split("::");
			valueMap.put(splitKeyValue[0], Double.valueOf(splitKeyValue[1]));
		}
	}

	public savedResultWrapper setId(int id) {
		this.id = id;
		return this;
	}

	public savedResultWrapper setTargetId(int targetId) {
		this.targetId = targetId;
		return this;
	}

	public savedResultWrapper setName(String name) {
		this.name = name;
		return this;
	}
	
	public savedResultWrapper setValueMap(HashMap<String, Double> valueMap) {
		this.valueMap = valueMap;
		return this;
	}
	
	@Override
	public String toString() {
		return "<save>\r\n<id>" + id + "</id>\r\n<targetId>" + targetId
				+ "</targetId>\r\n<name>" + name + "</name>\r\n<values>" + mapToString() + "</values>\r\n</save>\r\n";
	}
	
	public String mapToString(){
		String result = "";
		Set<String> keys = valueMap.keySet();
		for(String key : keys){
			result += key + "::" + valueMap.get(key) + ";;";
		}
		result = result.substring(0, result.length()-2);
		return result;
	}
	
}
